// pages/shoplist/shoplist.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    categories: [
      { id: 1, name: "饮料" },
      { id: 2, name: "食品" },
      { id: 3, name: "日用品" }
    ],
    goods: [
      { id: 101, name: "可乐", barcode: "123456", price: 3.5, categoryId: 1, image: "" },
      { id: 102, name: "矿泉水", barcode: "789012", price: 2.0, categoryId: 1, image: "" },
      { id: 201, name: "面包", barcode: "345678", price: 5.0, categoryId: 2, image: "" }
    ],
    activeCategoryId: 1,
    searchKey: '',
    openedId: '',
    name: '',
    goodsSource: 'plat'
  },

  onSearch(e) {
    this.setData({
      searchKey: e.detail.value
    });
  },

  onAdd() {
    wx.navigateTo({
      url: '/pages/shopdetaillist/shopdetaillist'
    });
  },

  onEdit(e) {
    console.log('编辑');
    console.log(e.detail.item);
    // 跳转至编辑页面
    wx.navigateTo({
      url: `/pages/shopedit/shopedit?name=${e.detail.item.name}&id=${e.detail.item.id}&price=${e.detail.item.price}&barcode=${e.detail.item.barcode}&sort=${e.detail.item.sort}&categoryId=${e.detail.item.categoryId}`
    });
  },

  onDelete(e) {
    console.log('删除');
    console.log(e.detail.id);
    // 删除商品
    wx.showModal({
      title: '提示',
      content: '确定删除商品吗？',
      complete: (res) => {
        if (res.cancel) {
          
        }
    
        if (res.confirm) {
          this.removeShopById(e.detail.id)
        }
      }
    })
  },

  collapseAll(e) {
    const { id } = e.detail;
    this.setData({ openedId: id });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getCagetoryList();
  },


  getCagetoryList(){
    let that = this
    that.requestHttp('/app/client/manage/goods/goods/category','get').then((data) => {
      console.log(data)
      console.log(data.data[0].id)
      that.setData({
        categories: data.data,
        activeCategoryId: data.data[0].id
      })
      that.getShopList()
    })
  },

  getShopList(){
    this.requestHttp('/app/client/manage/goods/pages', {'pageNum': 1, 'pageSize': 100, 'categoryId': this.data.activeCategoryId, 'name': this.data.name, 'goodsSource': this.data.goodsSource},'post').then((data) => {
      console.log(data)
      this.setData({
        goods: data.data.rows
      })
    })
  },

  onSearch(e) {
    const keyword = e.detail.value; // 获取输入的文字
    console.log('搜索关键字：', keyword);
    this.setData({
      name: keyword
    })
    this.getShopList()
  },

  goToDetail(e) {
    const item = e.currentTarget.dataset.item
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.emit('acceptDataFromOpenedPage', { item })
    wx.navigateBack()
  },

  onCategorySelect(e) {
    console.log(e.currentTarget.dataset.id)
    this.setData({
      activeCategoryId: e.currentTarget.dataset.id
    });
    this.getShopList()
  },

  removeShopById(id){
    let that = this
    that.requestHttp('/app/client/manage/goods/remove',{'ids': id},'get').then((data) => {
      console.log(data)
      if (data.data.code === 0) {
        wx.showToast({
          title: '删除商品成功',
        })
        that.getShopList()
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  requestHttp(url,data,method){
    return new Promise(function (reslove, reject) {
      let headers = {
        'Authorization': app.globalData.token || '',
        'Content-Type': 'application/x-www-form-urlencoded'
      };
      wx.request({
        url: app.globalData.host + url,
        method: method || 'get',
        data: data,
        header: headers,
        success: function success(res) {
          // const data = res.data;
          // if (data.code * 1 === 0) {
          //   reslove(data)
          // } else {
          //   wx.showToast({
          //     title: data.msg || '网络异常',
          //     icon: 'error',
          //   })
          //   setTimeout(() => {
          //     reject(data);
          //   }, 10)
          // }
          reslove(res)
        },
        fail: function fail(res) {
          wx.showToast({
            title: res,
            icon: 'error',
          })
          reject(res);
        }
      })
    })
  }
})